clear 
set more off
set maxvar 25000
set matsize 11000
**Aaron Flaaen
**July 25, 2013
**Last Updated: November 13, 2017
**This File Conducts Analysis Surrounding 2011 March Tohoku Event QUARTERLY FREQUENCY
**-------------------------------------------------------------------------

cd $dir
	
**--------------------------------------------------------------------------
**Step 1: Create Sample
**--------------------------------------------------------------------------
!gunzip analysisdata_manuf_Q.dta.gz
use analysisdata_manuf_Q.dta, clear

bys firmid: egen maxflagfor = max(flag_for_mult)
by firmid: egen maxflagus = max(flag_us_mult)
by firmid: egen maxflagjpn = max(japan)

replace flag_for_mult = maxflagfor
replace flag_us_mult = maxflagus
replace japan = maxflagjpn
drop maxflagjpn maxflagus maxflagfor


**Potential Sample 1: Only Other Multinationals
drop if flag_for_mult==0 & flag_us_mult==0

**Calculate Total Intermediate Input Imports
gen imp_int = njimp_int + jimp_int

**Drop if no Japanese affiliates in 4-digit industry (??????)
gen naics4 = substr(naics_code,1,4)

egen numnaics = group(naics4)



**--------------------------------------------------------------------------
**Step 2: Run Regressions
**--------------------------------------------------------------------------
**2.1 T-set data
gen qtime = 196
replace qtime = 197 if quarter==2 & year==2009
replace qtime = 198 if quarter==3 & year==2009
replace qtime = 199 if quarter==4 & year==2009
replace qtime = 200 if quarter==1 & year==2010
replace qtime = 201 if quarter==2 & year==2010
replace qtime = 202 if quarter==3 & year==2010
replace qtime = 203 if quarter==4 & year==2010
replace qtime = 204 if quarter==1 & year==2011
replace qtime = 205 if quarter==2 & year==2011
replace qtime = 206 if quarter==3 & year==2011
replace qtime = 207 if quarter==4 & year==2011
replace qtime = 208 if quarter==1 & year==2012
replace qtime = 209 if quarter==2 & year==2012
replace qtime = 210 if quarter==3 & year==2012
replace qtime = 211 if quarter==4 & year==2012

*drop year quarter
egen numfirm = group(firmid)
*drop firmid
tsset numfirm qtime, quarterly



**Step 5.2 Do the Re-weighting
gen presample = 0
replace presample = 1 if qtime==203 | qtime==202

bys firmid presample: egen presamp_naexp = mean(naexp)
bys firmid presample: egen presamp_imp = mean(imp_int)
bys firmid presample: egen presamp_njimp = mean(njimp_int)
bys firmid presample: egen presamp_emp = mean(emp)
bys firmid presample: egen presamp_pay = mean(pay)

replace presamp_imp = . if presample==0
replace presamp_naexp = . if presample==0
replace presamp_njimp = . if presample==0
replace presamp_emp = . if presample==0
replace presamp_pay = . if presample==0

bys firmid: egen presamp_naexp2 = min(presamp_naexp)
bys firmid: egen presamp_imp2 = min(presamp_imp)
bys firmid: egen presamp_njimp2 = min(presamp_njimp)
bys firmid: egen presamp_emp2 = min(presamp_emp)
bys firmid: egen presamp_pay2 = min(presamp_pay)

drop presamp_naexp
rename presamp_naexp2 presamp_naexp
drop presamp_imp
rename presamp_imp2 presamp_imp
drop presamp_njimp
rename presamp_njimp2 presamp_njimp
drop presamp_emp
rename presamp_emp2 presamp_emp
drop presamp_pay
rename presamp_pay2 presamp_pay

**Just using the probit
*probit japan i.numnaics presamp_imp presamp_naexp presamp_emp presamp_pay
probit japan i.numnaics  presamp_emp presamp_pay
predict jpnhat

gen jpnhat3 = jpnhat / (1-jpnhat)
replace jpnhat3 = 1 if japan==1






xi i.qtime*i.japan, noomit
forvalues j = 196(1)211 {
	rename _Iqtime_`j' qtime_`j'
	rename _IqtiXjap_`j'_1 qtiXjap_`j'
}

drop *_0

/*
**Remove Unit-Specific Trend for some variables
foreach var of varlist naexp njexp jimp_int imp_int {

	qui xi: reg `var' c.qtime##i.numfirm if qtime<xxx & numfirm<xxx
	qui predict `var'_pred if numfirm<xxx
	replace `var'_pred = . if numfirm>=xXX

	qui sum numfirm
	local ubound = floor(`r(max)'/1000)*10

	forvalues j = 5(5)`ubound' {
		local i = `j'*100
		local k = (`j'+5)*100
		di "`i'"
	
		qui xi: reg `var' c.qtime##i.numfirm if qtime<205 & numfirm>=`i' & numfirm<`k'
		qui predict `var'_`j'_pred if numfirm>=`i' & numfirm<`k'
		replace `var'_`j'_pred = . if numfirm<`i' & numfirm>=`k'
		replace `var'_pred = `var'_`j'_pred if `var'_pred==.
		drop `var'_`j'_pred
	}

	gen `var'_resid = `var' - `var'_pred
}

*/
tsset numfirm qtime, quarterly


**Employment Regressions
gen lemp = log(emp)
gen ldemp = d.lemp
gen ld4emp = lemp - l4.lemp
gen lpay = log(pay)
gen ldpay = d.lpay
gen ld4pay = lpay - l4.lpay


**2.1 Full Sample
**-------------------------------------------

capture erase ../Tables/qregs.txt

**3) Employment (Through 2011)
areg emp qtime_197-qtime_207 qtiXjap_197-qtiXjap_207 if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster  addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)
areg lemp qtime_197-qtime_207 qtiXjap_197-qtiXjap_207  if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)
areg ldemp qtime_198-qtime_207 qtiXjap_198-qtiXjap_207  if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)
areg ld4emp qtime_201-qtime_207 qtiXjap_201-qtiXjap_207  if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)

**Reweighted
areg emp qtime_197-qtime_207 qtiXjap_197-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster  addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)
areg lemp qtime_197-qtime_207 qtiXjap_197-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)
areg ldemp qtime_198-qtime_207 qtiXjap_198-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)
areg ld4emp qtime_201-qtime_207 qtiXjap_201-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)

**4) Payroll (Through 2011)
areg pay qtime_197-qtime_207 qtiXjap_197-qtiXjap_207 if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)
areg lpay qtime_197-qtime_207 qtiXjap_197-qtiXjap_207 if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)
areg ldpay qtime_198-qtime_207 qtiXjap_198-qtiXjap_207 if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)
areg ld4pay qtime_201-qtime_207 qtiXjap_201-qtiXjap_207 if qtime<208, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2011)

**reweighted
areg pay qtime_197-qtime_207 qtiXjap_197-qtiXjap_207  if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight- Mult-2009-2011)
areg lpay qtime_197-qtime_207 qtiXjap_197-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)
areg ldpay qtime_198-qtime_207 qtiXjap_198-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)
areg ld4pay qtime_201-qtime_207 qtiXjap_201-qtiXjap_207 if qtime<208 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2011)


**3) Employment (Through 2012)
areg emp qtime_197-qtime_211 qtiXjap_197-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)
areg lemp qtime_197-qtime_211 qtiXjap_197-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)
areg ldemp qtime_198-qtime_211 qtiXjap_198-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)
areg ld4emp qtime_201-qtime_211 qtiXjap_201-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)

**reweight
areg emp qtime_197-qtime_211 qtiXjap_197-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)
areg lemp qtime_197-qtime_211 qtiXjap_197-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)
areg ldemp qtime_198-qtime_211 qtiXjap_198-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)
areg ld4emp qtime_201-qtime_211 qtiXjap_201-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)

**4) Payroll (Through 2012)
areg pay qtime_197-qtime_211 qtiXjap_197-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)
areg lpay qtime_197-qtime_211 qtiXjap_197-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)
areg ldpay qtime_198-qtime_211 qtiXjap_198-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)
areg ld4pay qtime_201-qtime_211 qtiXjap_201-qtiXjap_211, absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Mult-2009-2012)

**Reweight
areg pay qtime_197-qtime_211 qtiXjap_197-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)
areg lpay qtime_197-qtime_211 qtiXjap_197-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)
areg ldpay qtime_198-qtime_211 qtiXjap_198-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)
areg ld4pay qtime_201-qtime_211 qtiXjap_201-qtiXjap_211 [aweight=jpnhat3], absorb(numfirm) vce(cluster numfirm)
outreg2 using ../Tables/qregs.txt, noparen noaster addtext(Fixed Effects, Firm, Clustering, Firm, Sample, Weight-Mult-2009-2012)



preserve
clear
import delimited qregs.txt
export excel using "qregs", sheet("T1_regs") sheetmodify firstrow(variables)
restore
